
import UIKit
class PzDefCoinNumView: UIView {
    lazy var defCoinNumHeadLab: UILabel = {
        let lab = UILabel()
        lab.text = "抛硬币设置"
        lab.font = UIFont.systemFont(ofSize: 36.fitScale(), weight: .regular)
        lab.textColor = UIColor(hex:0x333333)
        lab.textAlignment = .center
        return lab
    }()
    lazy var defCoinNumCountHeadLab: UILabel = {
        let lab = UILabel()
        lab.text = "次数"
        lab.font = UIFont.systemFont(ofSize: 30.fitScale(), weight: .regular)
        lab.textColor = UIColor(hex:0x333333)
        lab.textAlignment = .center
        return lab
    }()
    lazy var defCoinNumCountField: UITextField = {
        let textField = UITextField()
        textField.font = UIFont.systemFont(ofSize: 40.fitScale(), weight: .regular)
        textField.textColor = UIColor(hex:0x2D353B)
        textField.textAlignment = .right
        textField.keyboardType = .numberPad
        return textField
    }()
    lazy var defCoinNumCountLineView: UIView = {
        let defCoinNumCountLineView = UIView()
        defCoinNumCountLineView.backgroundColor = UIColor(hex:0xE5E5E5)
        return defCoinNumCountLineView
    }()
    lazy var defCoinNumTypeHeadLab: UILabel = {
        let lab = UILabel()
        lab.text = "自动抛"
        lab.font = UIFont.systemFont(ofSize: 30.fitScale(), weight: .regular)
        lab.textColor = UIColor(hex:0x333333)
        lab.textAlignment = .center
        return lab
    }()
    lazy var defCoinNumTypeYesBtn: PzCoinTypeBtn = {
        let btn = PzCoinTypeBtn(frame: .zero)
        btn.setTitle("是", for: .normal)
        btn.addTarget(self, action: #selector(defCoinNumTypeYes), for: .touchUpInside)
        return btn
    }()
    lazy var defCoinNumTypeNoBtn: PzCoinTypeBtn = {
        let btn = PzCoinTypeBtn(frame: .zero)
        btn.setTitle("否", for: .normal)
        btn.addTarget(self, action: #selector(defCoinNumTypeNo), for: .touchUpInside)
        return btn
    }()
    lazy var defCoinNumTypeLineView: UIView = {
        let defCoinNumTypeLineView = UIView()
        defCoinNumTypeLineView.backgroundColor = UIColor(hex:0xE5E5E5)
        return defCoinNumTypeLineView
    }()
    lazy var defCoinNumVoiceHeadLab: UILabel = {
        let lab = UILabel()
        lab.text = "开启音频"
        lab.font = UIFont.systemFont(ofSize: 30.fitScale(), weight: .regular)
        lab.textColor = UIColor(hex:0x333333)
        lab.textAlignment = .center
        return lab
    }()
    lazy var defCoinNumVoiceBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "pzCoinClose"), for: .normal)
        btn.setImage(UIImage(named: "pzCoinOpen"), for: .selected)
        btn.addTarget(self, action: #selector(defCoinNumVoice), for: .touchUpInside)
        return btn
    }()
    lazy var defCoinNumVoiceLineView: UIView = {
        let defCoinNumVoiceLineView = UIView()
        defCoinNumVoiceLineView.backgroundColor = UIColor(hex:0xE5E5E5)
        return defCoinNumVoiceLineView
    }()
    lazy var defCoinNumCancleBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("取消", for: .normal)
        btn.setTitleColor(UIColor(hex:0x666666), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 28.fitScale(), weight: .regular)
        btn.addTarget(self, action: #selector(defCoinNumCancle), for: .touchUpInside)
        return btn
    }()
    lazy var defCoinNumSureBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("确定", for: .normal)
        btn.setTitleColor(UIColor(hex:0x2194FF), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 28.fitScale(), weight: .regular)
        btn.addTarget(self, action: #selector(defCoinNumSure), for: .touchUpInside)
        return btn
    }()
    lazy var defCoinNumLineView: UIView = {
        let defCoinNumLineView = UIView()
        defCoinNumLineView.backgroundColor = UIColor(hex:0xE5E5E5)
        return defCoinNumLineView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.layer.cornerRadius = 20.fitScale()
        self.clipsToBounds = true
        creatSubView();
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func creatSubView() {
        self.addSubview(defCoinNumHeadLab)
        self.addSubview(defCoinNumCountHeadLab)
        self.addSubview(defCoinNumCountField)
        self.addSubview(defCoinNumCountLineView)
        
        self.addSubview(defCoinNumTypeHeadLab)
        self.addSubview(defCoinNumTypeYesBtn)
        self.addSubview(defCoinNumTypeNoBtn)
        self.addSubview(defCoinNumTypeLineView)
        
        self.addSubview(defCoinNumVoiceHeadLab)
        self.addSubview(defCoinNumVoiceBtn)
        self.addSubview(defCoinNumVoiceLineView)
        
        self.addSubview(defCoinNumCancleBtn)
        self.addSubview(defCoinNumSureBtn)
        self.addSubview(defCoinNumLineView)
        
        defCoinNumHeadLab.snp.makeConstraints { make in
            make.left.equalTo(0.fitScale())
            make.right.equalTo(0.fitScale())
            make.top.equalTo(51.fitScale())
            make.height.equalTo(43.fitScale())
        }
        defCoinNumCountHeadLab.snp.makeConstraints { make in
            make.left.equalTo(40.fitScale())
            make.top.equalTo(160.fitScale())
            make.height.equalTo(36.fitScale())
        }
        defCoinNumCountField.snp.makeConstraints { make in
            make.right.equalTo(-40.fitScale())
            make.centerY.equalTo(defCoinNumCountHeadLab)
            make.width.equalTo(180.fitScale())
            make.height.equalTo(56.fitScale())
        }
        let defCoinNumCount:String? = UserDefaults.standard.value(forKey: "pzDefNum") as? String
        defCoinNumCountField.text = defCoinNumCount
        
        defCoinNumCountLineView.snp.makeConstraints { make in
            make.left.equalTo(13.fitScale())
            make.right.equalTo(-13.fitScale())
            make.top.equalTo(232.fitScale())
            make.height.equalTo(1)
        }
        defCoinNumTypeHeadLab.snp.makeConstraints { make in
            make.left.equalTo(40.fitScale())
            make.top.equalTo(269.fitScale())
            make.height.equalTo(36.fitScale())
        }
        let defAutoNum:String? = UserDefaults.standard.value(forKey: "pzDefAutoNum") as? String
        if (defAutoNum == "1"){
            defCoinNumTypeNoBtn.isSelected = true
            defCoinNumTypeYesBtn.isSelected = false
        }else{
            defCoinNumTypeNoBtn.isSelected = false
            defCoinNumTypeYesBtn.isSelected = true
        }
        defCoinNumTypeNoBtn.snp.makeConstraints { make in
            make.right.equalTo(-29.fitScale())
            make.centerY.equalTo(defCoinNumTypeHeadLab)
//            make.width.equalTo(60.fitScale())
            make.height.equalTo(36.fitScale())
        }
        defCoinNumTypeYesBtn.snp.makeConstraints { make in
            make.right.equalTo(defCoinNumTypeNoBtn.snp.left).offset(-100.fitScale())
            make.centerY.equalTo(defCoinNumTypeHeadLab)
//            make.width.equalTo(60.fitScale())
            make.height.equalTo(36.fitScale())
        }
        defCoinNumTypeLineView.snp.makeConstraints { make in
            make.left.equalTo(13.fitScale())
            make.right.equalTo(-13.fitScale())
            make.top.equalTo(341.fitScale())
            make.height.equalTo(1)
        }
        defCoinNumVoiceHeadLab.snp.makeConstraints { make in
            make.left.equalTo(40.fitScale())
            make.top.equalTo(403.fitScale())
            make.height.equalTo(36.fitScale())
        }
        let defVoiceOpen:String? = UserDefaults.standard.value(forKey: "pzDefVoiceOpen") as? String
        if (defVoiceOpen == "1"){
            defCoinNumVoiceBtn.isSelected = false
        }else{
            defCoinNumVoiceBtn.isSelected = true
        }
        defCoinNumVoiceBtn.snp.makeConstraints { make in
            make.right.equalTo(-47.fitScale())
            make.centerY.equalTo(defCoinNumVoiceHeadLab)
            make.width.equalTo(92.fitScale())
            make.height.equalTo(56.fitScale())
        }
        defCoinNumVoiceLineView.snp.makeConstraints { make in
            make.left.equalTo(0.fitScale())
            make.right.equalTo(-0.fitScale())
            make.bottom.equalTo(-86.fitScale())
            make.height.equalTo(1)
        }
        defCoinNumCancleBtn.snp.makeConstraints { make in
            make.left.equalTo(-0.fitScale())
            make.bottom.equalTo(0.fitScale())
            make.height.equalTo(86.fitScale())
        }
        defCoinNumSureBtn.snp.makeConstraints { make in
            make.left.equalTo(defCoinNumCancleBtn.snp.right)
            make.right.equalTo(-0.fitScale())
            make.bottom.equalTo(0.fitScale())
            make.height.equalTo(86.fitScale())
            make.width.equalTo(defCoinNumCancleBtn.snp.width)
        }
        defCoinNumLineView.snp.makeConstraints { make in
            make.width.equalTo(1)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-0.fitScale())
            make.height.equalTo(86.fitScale())
        }
        let defCoinNumTap = UITapGestureRecognizer(target: self, action: #selector(defCoinNum))
        self.addGestureRecognizer(defCoinNumTap);
    }
    @objc func defCoinNumTypeYes(){
        defCoinNumTypeYesBtn.isSelected = true
        defCoinNumTypeNoBtn.isSelected = false
    }
    @objc func defCoinNumTypeNo(){
        defCoinNumTypeYesBtn.isSelected = false
        defCoinNumTypeNoBtn.isSelected = true
    }
    @objc func defCoinNum(){
        self.endEditing(true)
    }
    @objc func defCoinNumVoice(){
        defCoinNumVoiceBtn.isSelected = !defCoinNumVoiceBtn.isSelected
    }
    @objc func defCoinNumCancle(){
        defCoinNumViewPopUp?.dismiss()
    }
    @objc func defCoinNumSure(){
        UserDefaults.standard.setValue(defCoinNumCountField.text, forKey: "pzDefNum")
        if defCoinNumTypeYesBtn.isSelected{
            UserDefaults.standard.setValue("2", forKey: "pzDefAutoNum")
        }else{
            UserDefaults.standard.setValue("1", forKey: "pzDefAutoNum")
        }
        if defCoinNumVoiceBtn.isSelected{
            UserDefaults.standard.setValue("2", forKey: "pzDefVoiceOpen")
        }else{
            UserDefaults.standard.setValue("1", forKey: "pzDefVoiceOpen")
        }
        UserDefaults.standard.synchronize()
        defCoinNumFinishBlock?()
        defCoinNumViewPopUp?.dismiss()
    }
    
    var defCoinNumViewPopUp:zhPopupController?
    var defCoinNumFinishBlock : (()->Void)?
    @objc static func showDefCoinNumView(keyView:UIView,closure: @escaping ()->Void){
        let defCoinNumView = PzDefCoinNumView(frame: .zero)
        let defCoinNumViewPopUp = zhPopupController(view: defCoinNumView, size: CGSize(width: 694.fitScale(), height: 592.fitScale()))
        defCoinNumView.defCoinNumFinishBlock = closure
        defCoinNumView.defCoinNumViewPopUp = defCoinNumViewPopUp;
        defCoinNumView.defCoinNumViewPopUp?.presentationStyle = .transform;
        defCoinNumView.defCoinNumViewPopUp?.layoutType = .center;
        defCoinNumView.defCoinNumViewPopUp?.presentationTransformScale = 1.25;
        defCoinNumView.defCoinNumViewPopUp?.dismissonTransformScale = 0.85;
        defCoinNumView.defCoinNumViewPopUp?.maskAlpha = 0.6;
        defCoinNumView.defCoinNumViewPopUp?.dismissOnMaskTouched = false
        defCoinNumView.defCoinNumViewPopUp?.show(in: keyView, completion: {
            
        })
    }

}
